package com.example.Config;


import com.example.Task.TimerTaskTest1;
import com.example.Task.TimerTaskTest2;
import org.quartz.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


@Configuration
public class QuartzConfiguration {

    /**
     * 定义时间间隔方式
     * @return
     */
    @Bean
    public JobDetail testQuartz1() {
        return JobBuilder.newJob(TimerTaskTest1.class).withIdentity("testTask1").storeDurably().build();
    }

    @Bean
    public Trigger testQuartzTrigger1() {
        //5秒执行一次
        SimpleScheduleBuilder scheduleBuilder = SimpleScheduleBuilder.simpleSchedule()
                .withIntervalInSeconds(5)
                .repeatForever();
        return TriggerBuilder.newTrigger().forJob(testQuartz1())
                .withIdentity("testTask1")
                .withSchedule(scheduleBuilder)
                .build();
    }

    /**
     * cron表达式
     * @return
     */
    @Bean
    public JobDetail testQuartz2() {
        return JobBuilder.newJob(TimerTaskTest2.class).withIdentity("testTask2").storeDurably().build();
    }

    @Bean
    public Trigger testQuartzTrigger2() {
        //cron方式，每隔5秒执行一次
        return TriggerBuilder.newTrigger().forJob(testQuartz2())
                .withIdentity("testTask2","ydy")
                .withSchedule(CronScheduleBuilder.cronSchedule("* * 0/1 * * ? "))
                .build();
    }

}
